//
//  HUDUtil.m
//  ZKFrameWork
//
//  Created by 熊国兵 on 2018/4/4.
//  Copyright © 2018年 熊国兵. All rights reserved.
//

#import "HUDUtil.h"

@implementation HUDUtil

+ (void)initialize
{
    [SVProgressHUD setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.5]];
    [SVProgressHUD setForegroundColor:WhiteColor];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
    [SVProgressHUD setMaximumDismissTimeInterval:5];
}

+ (void)show {
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD show];
}

+ (void)showStr:(NSString *)str
{
    if ([SVProgressHUD isVisible]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            if (ValidStr(str)) {
                [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
                [SVProgressHUD showImage:nil status:str];
            }
        });
    }else{
        if (ValidStr(str)) {
            [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeNone];
            [SVProgressHUD showImage:nil status:str];
        }
    }
}

+ (void)loadingWithString:(NSString *)string
{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    if (!string.length) {
        [SVProgressHUD show];
        return;
    }
    [SVProgressHUD showWithStatus:string];
}

+ (void)loadingWithProgress:(CGFloat)progress
{
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    [SVProgressHUD showProgress:progress];
}

+ (void)dismiss
{
    if ([SVProgressHUD isVisible]) {
        [SVProgressHUD dismiss];
    }
}

//获取当前正在显示的 VC
+ (UIViewController *)topViewController
{
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

+ (UIViewController *)_topViewController:(UIViewController *)vc
{
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}

//获取当前控制器的导航栏
+ (UINavigationController *)getNav
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    UIViewController *vc = keyWindow.rootViewController;
    
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return (UINavigationController *)vc;
    }else if ([vc isKindOfClass:[UITabBarController class]]) {
        UIViewController *vc2 = [(UITabBarController *)vc selectedViewController];
        if ([vc2 isKindOfClass:[UINavigationController class]]) {
            return (UINavigationController *)vc2;
        }else if (vc2.navigationController){
            return vc2.navigationController;
        }
    } else if (vc.navigationController) {
        return vc.navigationController;
    }
    return nil;
}

+ (void)showWithTitle:(NSString *)title viewController:(UIViewController *)vc handler:(void(^)(UIAlertAction * action))handler
{
    [self showWithTitle:title message:nil viewController:vc handler:handler];
}

+ (void)showWithTitle:(NSString *)title message:(NSString *)message viewController:(UIViewController *)vc handler:(void(^)(UIAlertAction * action))handler
{
    [self showWithTitle:title message:message cancel:@"取消" sure:@"确定" viewController:vc handler:handler];
}

+ (void)showWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancelTitle sure:(NSString *)sureTitle viewController:(UIViewController *)vc handler:(void(^)(UIAlertAction * action))handler
{
    [self showWithTitle:title message:message cancel:cancelTitle sure:sureTitle viewController:vc cancelHandler:nil handler:handler];
}

+  (void)showSheetWithTitle:(NSString *)title message:(NSString *)message vc:(UIViewController *)vc indexBlock:(void(^)(NSInteger index))indexBlock cancelTitle:(NSString *)canceTitle otherTitle:(NSString *)otherTitle,...NS_REQUIRES_NIL_TERMINATION
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
    NSString *actionTitle = nil;
    va_list args;//用于指向第一个参数
    NSMutableArray *actionTitles = [NSMutableArray array];
    [actionTitles addObject:canceTitle];
    if (otherTitle) {
        [actionTitles addObject:otherTitle];
        va_start(args, otherTitle);//对args进行初始化，让它指向可变参数表里面的第一个参数
        while ((actionTitle = va_arg(args, NSString*))) {
            [actionTitles addObject:actionTitle];
        }
        va_end(args);
    }
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:canceTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cancelAction];
    
    for (int i = 0 ; i < actionTitles.count; i++) {
        if (i == 0)continue;
        NSString *actionTitle = actionTitles[i];
        UIAlertAction *action = [UIAlertAction actionWithTitle:actionTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (indexBlock) {
                indexBlock(i);
            }
        }];
        [alert addAction:action];
    }
    [vc presentViewController:alert animated:YES completion:nil];
}


+ (void)showWithTitle:(NSString *)title message:(NSString *)message cancel:(NSString *)cancelTitle sure:(NSString *)sureTitle viewController:(UIViewController *)vc cancelHandler:(void(^)(UIAlertAction * action))cancelHandler handler:(void(^)(UIAlertAction * action))handler
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:cancelHandler];
    UIAlertAction * sure = [UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDestructive handler:handler];
    [alert addAction:action];
    [alert addAction:sure];
    [action setValue:GrayLightColor forKey:@"_titleTextColor"];
    [sure setValue:ThemeColor forKey:@"_titleTextColor"];
    [vc presentViewController:alert animated:YES completion:nil];
}


///添加动画  摇动
+ (void)shakeWithView:(UIView *)view
{
    CGRect frame = view.frame;
    CAKeyframeAnimation *shakeAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    CGMutablePathRef shakePath = CGPathCreateMutable();
    CGPathMoveToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2, frame.origin.y+frame.size.height/2);
    int index;
    for (index = 3; index >=0; --index) {
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 - frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
        CGPathAddLineToPoint(shakePath, NULL, frame.origin.x+frame.size.width/2 + frame.size.width * 0.02f * index, frame.origin.y+frame.size.height/2);
    }
    CGPathCloseSubpath(shakePath);
    
    shakeAnimation.path = shakePath;
    shakeAnimation.duration = 0.5f;
    shakeAnimation.removedOnCompletion = YES;
    
    [view.layer addAnimation:shakeAnimation forKey:nil];
    CFRelease(shakePath);
}

//持续晃动动画
+ (void)continueShakeWithView:(UIView *)view
{
    //创建动画
    CAKeyframeAnimation * keyAnimaion = [CAKeyframeAnimation animation];
    keyAnimaion.keyPath = @"transform.rotation";
    keyAnimaion.values = @[@(-10 / 180.0 * M_PI),@(10 /180.0 * M_PI),@(-10/ 180.0 * M_PI)];//度数转弧度
    
    keyAnimaion.removedOnCompletion = NO;
    keyAnimaion.fillMode = kCAFillModeForwards;
    keyAnimaion.duration = 0.3;
    keyAnimaion.repeatCount = MAXFLOAT;
    [view.layer addAnimation:keyAnimaion forKey:@"continueShake"];
}

//移除动画
+ (void)removeShakeWithView:(UIView *)view
{
    NSString * key = @"continueShake";
    if ([view.layer animationForKey:key]) {
        [view.layer removeAnimationForKey:key];
    }
}



@end
